#include "widget.h"
#include "ui_widget.h"
#include <QShortcut>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //实例：通过快捷键来控制滑块的移动
    //首先创建快捷键的对象
    QShortcut * cut1 = new QShortcut(this);
    cut1->setKey(QKeySequence("-"));
    connect(cut1,&QShortcut::activated,this,&Widget::suber);

    QShortcut *cut2 = new QShortcut(this);
    cut2->setKey(QKeySequence("="));
    connect(cut2,&QShortcut::activated,this,&Widget::add);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::suber()
{
    int value = ui->horizontalSlider->value();
    if(value<=ui->horizontalSlider->minimum())
    {
        return;
    }
    ui->horizontalSlider->setValue(value-5);
}

void Widget::add()
{
    int value = ui->horizontalSlider->value();
    if(value>=ui->horizontalSlider->maximum())
    {
        return;
    }
    ui->horizontalSlider->setValue(value+5);
}


void Widget::on_horizontalSlider_valueChanged(int value)
{
    ui->label->setText("当前滑动条的数值为："+QString::number(value));
}
